//
//  VisitorLoginView.swift
//  Weibo
//
//  Created by 张远文 on 16/4/25.
//  Copyright © 2016年 张远文. All rights reserved.
//

import UIKit

@objc protocol VisitorLoginViewDelegete : NSObjectProtocol {
    
    //登录
    func VisitorWillLogin()
    
    //注册
    optional func VisitorWillRegister()
}

class VisitorLoginView: UIView {

    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    weak var visitorDelegete: VisitorLoginViewDelegete?
    
    //按钮触发方法
    @objc func loginDidBtnClick() {
        
        visitorDelegete?.VisitorWillLogin()
    }
    
    @objc func registerDidBtnClick() {
        
        visitorDelegete?.VisitorWillRegister?()
    }
    
    //设置页面
    func setUIInfo(imageName: String?, title: String) {
        
        iconView.hidden = false
        tipLabel.text = title
        if imageName != nil {
            circleView.image = UIImage(named: imageName!)
            bringSubviewToFront(circleView)
            iconView.hidden = true
        } else {
            startAnimation()
        }
    }
    
    //设置动画
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        anim.duration = 20
        
        anim.removedOnCompletion = false
        
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    
    //设置访客视图
    private func setupUI() {
        
        //添加子视图
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //设置frame布局失效
        for subview in subviews {
            subview.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //设置约束
        //"view1.attr1 = view2.attr2 * multiplier + constant"
        //设置圆圈约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        
        //设置图标约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //设置提示文案约束
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 26))
        //设置宽度约束
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50))
        
        //设置登录按钮约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        //设置宽度约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //设置注册按钮约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        //设置宽度约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //设置背景约束
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView" : backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-45)-[registerBtn]", options: [], metrics: nil, views: ["backView" : backView,"registerBtn" : registerBtn]))
        
        //设置背景颜色
        backgroundColor = UIColor(white: 0.93,alpha: 1)
        
        //添加点击事件
        loginBtn.addTarget(self, action: #selector(loginDidBtnClick), forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: #selector(registerDidBtnClick), forControlEvents: .TouchUpInside)
    }
    
    //懒加载所有控件
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //提示文案
    private lazy var tipLabel: UILabel = UILabel(title: "关注一些人,回这里看看有什么,惊喜关注一些人,回这里看看有什么惊喜", size: 14, color: UIColor.lightGrayColor())
//    private lazy var tipLabel: UILabel = {
//        
//        let tip = UILabel()
//        
//        tip.text = "关注一些人,回这里看看有什么,惊喜关注一些人,回这里看看有什么惊喜"
//        tip.textAlignment = NSTextAlignment.Center
//        tip.font = UIFont.systemFontOfSize(14)
//        tip.textColor = UIColor.lightGrayColor()
//        tip.numberOfLines = 0
//        
//        return tip
//    }()

    //按钮
    private lazy var loginBtn: UIButton = UIButton(title: "登录", backImage: "common_button_white_disable", color: UIColor.orangeColor())
//    private lazy var loginBtn: UIButton = {
//        
//        let btn = UIButton()
//        
//        btn.setTitle("登录", forState: UIControlState.Normal)
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
//        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
//        
//        btn.addTarget(self, action: #selector(loginDidBtnClick), forControlEvents: .TouchUpInside)
//        
//        return btn
//    }()
    
    private lazy var registerBtn: UIButton = UIButton(title: "注册", backImage: "common_button_white_disable", color: UIColor.orangeColor())
//    private lazy var registerBtn: UIButton = {
//       
//        let btn = UIButton()
//        
//        btn.setTitle("注册", forState: UIControlState.Normal)
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
//        
//        btn.addTarget(self, action: #selector(registerDidBtnClick), forControlEvents: .TouchUpInside)
//        
//        return btn
//    }()
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
